package com.example.teacher.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.constants.RdsConstants;
import com.example.common.domain.pojo.Student;
import com.example.common.utils.CacheClient;
import com.example.teacher.Service.StudentService;
import com.example.teacher.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Autowired
    private CacheClient cacheClient;

    @Override
    public List<Student> getStudentByIdWithLogicalExpireIds(List<String> ids) {
        // 检查参数是否为空
        if (ids == null || ids.isEmpty()) {
            return Collections.emptyList();
        }
        // 创建查询条件
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        // 添加 IN 条件
        queryWrapper.in(Student::getId, ids);

        // 执行查询并返回结果
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public Student getStudentByIdWithLogicalExpire(String id) {
        return cacheClient.queryWithLogicalExpire(
                RdsConstants.STUDENT_INFO,
                id,
                Student.class,
                this::getById,
                RdsConstants.CACHE_TTL,
                TimeUnit.MINUTES,
                false
        );
    }

}
